<template>
  <!--批量活动执行情况查询厂端-->
  <div>
    <TopFilter ref="topfilter" :list="topFilterList" :cols="4" labelWidth="100" :collapse="true" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      :isExportExcel="false"
      exportFileName="批量活动执行情况查询.xlsx"
      :onCalcExportData="handleExportFun"
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      datakey="records"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls">
        <!-- <multiuse-button type="primary" :click="exportHandle.bind(this)">导出</multiuse-button> -->
      </template>
    </FilterTable>

    <Drawer :visible.sync="visibleDetail" :title="dialogTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <detail :row="formData" :maId="maId" @close="closeHandler" :handleType="handleType" />
    </Drawer>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import detail from './detail';
import { isEmpty } from '@/utils/utils';
import {exportDMaExecuteCondition,getExecutionStatus } from '@/api/repair/batchEventManagement/activityExecutionStatusQueryCDuan';
import { arrayObjDeWeight } from '@/utils';
import { dealerMixin } from '@/pages/repair/batchEventManagement/dealerMixin';

export default {
  name: 'index',
  mixins: [dictionary, dealerMixin],
  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '审核' };
    return {
      topFilterList: this.createTopFilterList(),
      visible: false,
      fetchApi: getExecutionStatus,
      dialogTitle: '',
      fetchParams: { xhrAbort: true },
      showDetail: false,
      visibleDetail:false,
      rowData: {},
      formData:{},
      columns: this.createTableColumns(),
      handleType: '',
      type: 1,
      resDealer: '',
      /*执行经销商*/
      ownerCode: '',
      maId:''
    };
  },
  components: {
    detail
  },

  mounted() {
    // this.getAllDelears(this.columns, ['ownerCode'], 'table');

    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    async exportHandle() {
      // let json = Object.assign({}, { CODE: 'CODE_00000134' }, this.fetchParams);
      // let res = await exporturl(json);
      // if (res.resultCode == 200) {
      //   this.$router.push('/admin/auth/oem/downloadCenter');
      // }
      const [err, params] = await this.$refs.topfilter.GET_FORM_DATA();

      await this.downHandleFile(exportDMaExecuteCondition, params, '批量活动执行情况查询');
    },

    selectFun(list) {
      let row = list[0];
      if (this.type == 1) {
        //经销商
        // this.resDealer = row['dealerCode'];
        // this.topFilterList.find(item => item.fieldName == 'resDealerName').initialValue = row['dealerName'];
      } else {
        //执行经销商
        this.ownerCode = row['dealerCode'];
        this.topFilterList.find(item => item.fieldName == 'ownerName').initialValue = row['dealerName'];
      }
    },

    // 导出处理函数
    handleExportFun(row) {
      row.roStatus = this.createDictText(row.roStatus, 8049);
    },

    /*1 经销商查询 2 执行经销商*/
    seachFun(type) {
      this.type = type;
      this.visible = true;
    },

    closeDialog() {
      this.visible = false;
    },

    async initSelect() {
      let res = await getAllUser();
      // 去重
      let dataList = arrayObjDeWeight(res.data, 'userId');

      this.columns.find(item => item.dataIndex === 'maBy').dictItems = dataList.map(x => ({
        text: x.employeeName,
        value: x.userId
      }));
    },

    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 85,
          render: props => {
            return (
              <div>
                <el-button size="small" type="text" onClick={() => this.detailHandle(props.row)}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '活动编号',
          dataIndex: 'maNo',
          width: 150,
          sorter: true
        },

        {
          title: '活动名称',
          dataIndex: 'maTitle',
          sorter: true
        },
         {
          title: '活动类型',
          dataIndex: 'maStrategy',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(5080),
          render: props => {
            return <span>{this.createDictText(props.row.maStrategy, 5080)}</span>;
          }
        },
        {
          title: '活动开始时间',
          dataIndex: 'bDate',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '活动结束时间',
          dataIndex: 'eDate',
          editType: 'date-picker',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '活动总数量',
          dataIndex: 'counts',
          sorter: true
        },
        {
          title: '完成数量',
          dataIndex: 'completedCount',
          sorter: true
        },
        {
          title: '未完成数量',
          dataIndex: 'unCompletedCount',
          sorter: true
        },
      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '活动编号',
          fieldName: 'maNo',
          placeholder: '活动编号...'
        },
        {
          type: 'INPUT',
          label: '活动名称',
          fieldName: 'maTitle',
          placeholder: '活动名称...'
        },
        {
          type: 'SELECT',
          label: '活动类型',
          fieldName: 'maStrategy',
          placeholder: '请选择',
          itemList: this.createDictList(5080)
        },
      ];
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    changeHandle(val) {
      // 重置操作还没完成，就来获取initialValue，从val拿是准的，从页面拿不行
      // let ownerName = this.topFilterList.find(x => x.fieldName === 'ownerName').initialValue;
      let ownerName = val['ownerName'];
      if (ownerName && ownerName.length) {
        val['ownerCode'] = this.ownerCode;
      } else {
        this.ownerCode = undefined;
        val['ownerCode'] = undefined;
      }
      this.fetchParams = Object.assign({ xhrAbort: false }, val);

      console.log(this.fetchParams);
    },
    detailHandle(row) {
      this.maId = row['maId'];
      this.formData = row;
      this.handleType = 'show';
      this.showHandler();
    },
    showHandler() {
      this.visibleDetail = true;
    },
    closeHandler(type) {
      this.visibleDetail = false;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
